package com.xhh.nphm.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.sun.jna.platform.win32.AccCtrl;
import com.xhh.nphm.common.annotation.RepeatSubmit;
import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.AjaxResult;
import com.xhh.nphm.common.core.domain.entity.SysUser;
import com.xhh.nphm.common.core.page.TableDataInfo;
import com.xhh.nphm.common.utils.DateUtils;
import com.xhh.nphm.common.utils.ShiroUtils;
import com.xhh.nphm.system.domain.*;
import com.xhh.nphm.system.service.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/mInterior")
public class MInteriorCmnFileController extends BaseController {
    private String prefix = "system/mInterior";

    @Autowired
    private ITWfDeferService wfDeferService;
    @Autowired
    private IWfSendBackService wfSendBackService;
    @Autowired
    private ITworkFormLogService iTworkFormLogService;
    @Autowired
    private IWorkFormBaseService workFormBaseService;
    @Autowired
    private ICommonService commonService;
    @Autowired
    private IMwfDeferService mwfDeferService;
    @Autowired
    private MmailWorkFormService mmailWorkFormService;
    @Autowired
    private MwfSendBackService mwfSendBackService;
    @Autowired
    private MMailWorkFormLogService mMailWorkFormLogService;



    /**
     * 延期申请结果T列表页面
     *
     * @author zz
     * @return 视图对象
     */
    @GetMapping("/cmnFile/tDeferResult")
    public ModelAndView tDeferResult() {
        return new ModelAndView(prefix + "/cmnFile/tDeferResult");
    }

    /**
     * 延期申请结果T列表数据
     *
     * @author zz
     * @return 视图对象
     */
    @PostMapping("/cmnFile/tDeferResultJson")
    public TableDataInfo tDeferResultJson(TwfDeferEty twfDeferEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<TwfDeferEty> list = wfDeferService.tDeferResultJson(twfDeferEty, user.getDeptId().toString());
        return getDataTable(list);
    }


    /**
     * 延期申请结果M列表页面
     *
     * @author zz
     * @return 视图对象
     */
    @GetMapping("/cmnFile/mDeferResult")
    public ModelAndView mDeferResult() {
        return new ModelAndView(prefix + "/cmnFile/mDeferResult");
    }

    /**
     * 延期申请结果M列表数据
     *
     * @author zz
     * @return 视图对象
     */
    @PostMapping("/cmnFile/mDeferResultJson")
    public TableDataInfo mDeferResultJson(MwfDeferEty mwfDeferEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<MwfDeferEty> list = mwfSendBackService.mDeferResultJson(mwfDeferEty, user.getDeptId().toString());
        return getDataTable(list);
    }

    /**
     * 延期申请-已审 列表页面
     *
     * @param
     * @return
     */
    @GetMapping("/cmnFile/tDeferChkN")
    public ModelAndView tDeferChkN() {
        return new ModelAndView(prefix + "/cmnFile/tDeferChkNList");
    }

    /**
     * 延期申请-已审列表数据
     *
     * @return
     */
    @PostMapping("/cmnFile/tDeferChkNJson")
    public TableDataInfo tDeferChkNJson(TwfDeferEty twfDeferEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<TwfDeferEty> list = wfDeferService.tDeferChkNJson(twfDeferEty, user.getDeptId().toString());
        return getDataTable(list);
    }

    /**
     * 延期申请-未审 列表页面
     *
     * @param
     * @return
     */
    @GetMapping("/cmnFile/tDeferUntried")
    public ModelAndView tDeferUntried() {
        return new ModelAndView(prefix + "/cmnFile/tDeferUntriedList");
    }

    /**
     * 延期申请-未审列表数据
     *
     * @return
     */
    @PostMapping("/cmnFile/tDeferUntriedJson")
    public TableDataInfo tDeferUntriedJson(TwfDeferEty twfDeferEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<TwfDeferEty> list;
        if (IS_GAOXIN()){
            list = wfDeferService.tDeferUntriedJson_gxq(twfDeferEty, user.getDeptId().toString());
        }else{
            list = wfDeferService.tDeferUntriedJson(twfDeferEty, user.getDeptId().toString());
        }
        return getDataTable(list);
    }

    /**
     * 延期申请情况页面
     */
    @GetMapping("/cmnFile/tDeferList")
    public ModelAndView tDeferList(Integer wfId, Integer sendId, ModelMap map) {
        map.addAttribute("wfId", wfId);
        map.addAttribute("sendId", sendId);
        return new ModelAndView(prefix + "/cmnFile/twfDeferList");
    }

    /**
     * 延期申请情况页面列表数据
     *
     * @param sendId 拟派工单id
     * @return
     */
    @PostMapping("/cmnFile/tDeferListJson")
    public TableDataInfo tDeferListJson(String sendId, String sdeptName, String delayRN) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list = wfDeferService.twfDeferListJson(sendId, sdeptName, delayRN);
        return getDataTable(list);
    }

    /**
     * 延期申请处理页面
     *
     * @param
     * @return
     */
    @GetMapping("/cmnFile/twfDeferList/{pkId}")
    public ModelAndView twfDeferList(@PathVariable("pkId") Integer pkId, ModelMap map) {
        SysUser user = ShiroUtils.getSysUser();
        TwfDeferEty twfDeferEty = wfDeferService.getById(pkId);
        twfDeferEty.setChkUserName(user.getUserName());
        twfDeferEty.setChkUserNo(user.getUserNo());
        TworkFormBaseEty tworkFormBaseEty = workFormBaseService.getById(twfDeferEty.getWfId());
        TwfSendBackEty twfSendBackEties = wfSendBackService.getById(twfDeferEty.getSendId());
        if (tworkFormBaseEty != null) {
            map.put("fmContent", tworkFormBaseEty.getFmContent());
            map.put("topic", tworkFormBaseEty.getWsTopic());
        }
        if (twfSendBackEties != null) {
            map.put("sendDesc", twfSendBackEties.getSendDesc());
        }
        map.put("twfDefer", twfDeferEty);
        map.put("delayRn", twfDeferEty.getDelayRn());
        map.put("adeptName", twfDeferEty.getAdeptName());
        map.put("auserName", twfDeferEty.getAuserName());
        map.put("limitOld", twfDeferEty.getLimitOld());
        return new ModelAndView(prefix + "/cmnFile/tDeferCheck");
    }

    /**
     * 延期申请列表数据
     */
//    @PostMapping("/cmnFile/twfDeferListJson")
//    public TableDataInfo twfDeferListJson(Integer wfId) {
//        startPage();
//        List<TwfDeferEty> list = wfDeferService.twfDeferListJson(wfId);
//        return getDataTable(list);
//    }

    /**
     * 新增延期申请页面
     *
     * @param wfId   工单id
     * @param sendId 派单id
     * @return
     */
    @GetMapping("/cmnFile/twfDeferAdd")
    public ModelAndView twfDeferAdd(Integer wfId, Integer sendId, ModelMap map) {
        //查询工单信息
        TworkFormBaseEty tworkFormBaseEty = workFormBaseService.selectByPkId(wfId);
        //查询派单信息
        TwfSendBackEty twfSendBackEty = wfSendBackService.selectByPkId(sendId);
        //获取当前用户
        SysUser user = ShiroUtils.getSysUser();
        //向页面响应所需显示的数据
        map.put("wfId", wfId);
        map.put("sendId", sendId);
        map.put("tworkFormBase", tworkFormBaseEty);
        map.put("twfSendBack", twfSendBackEty);
        map.put("user", user);
        return new ModelAndView(prefix + "/cmnFile/twfDeferAdd");
    }

    /**
     * 延期申请新增数据
     *
     * @return
     */
    @PostMapping("/cmnFile/twfDeferAddJson")
    @RequestBody
    @RepeatSubmit
    public AjaxResult twfDeferAddJson(TwfDeferEty twfDeferEty) {
        //记录 新增延期申请 操作日志
        addDeferLog(getSysUser(),twfDeferEty);
        //新增延期申请数据
        return toAjax(wfDeferService.save(twfDeferEty));
    }

    /**
     * 延期申请编辑页面
     *
     * @param
     * @return
     */
    @GetMapping("/cmnFile/twfDeferEdit/{pkId}")
    public ModelAndView twfDeferEdit(@PathVariable Integer pkId,@RequestParam(value="act",required = false,defaultValue = "1") int act, ModelMap map) {
        TwfDeferEty twfDeferEty = wfDeferService.getById(pkId);
        //查询工单信息
        TworkFormBaseEty tworkFormBaseEty = workFormBaseService.selectByPkId(twfDeferEty.getWfId());
        //查询派单信息
        TwfSendBackEty twfSendBackEty = wfSendBackService.selectByPkId(twfDeferEty.getSendId());
        map.put("twfDefer", twfDeferEty);
        //向页面响应所需显示的数据
        map.put("tworkFormBase", tworkFormBaseEty);
        map.put("twfSendBack", twfSendBackEty);
        map.put("act",act);
        return new ModelAndView(prefix + "/cmnFile/twfDeferEdit");
    }

    /**
     * 延期申请编辑数据
     *
     * @return
     */
    @PostMapping("/cmnFile/twfDeferEditJson")
    @RequestBody
    @RepeatSubmit
    public AjaxResult twfDeferEditJson(TwfDeferEty twfDeferEty) {
        return toAjax(wfDeferService.updateById(twfDeferEty));
    }

    /**
     * 延期申请
     * 根据id 进行多个删除和单个删除
     *
     * @return
     */
    @PostMapping("/cmnFile/removetwfDeferJson")
    @RequestBody
    public AjaxResult removetwfDeferJson(String... ids) {
        return toAjax(wfDeferService.deleteByIds(ids));
    }

    /**
     * 延期申请 通过或者不通过
     *
     * @return
     */
    @PostMapping("/cmnFile/tDeferCheckJson")
    @RequestBody
    public AjaxResult tDeferCheckJson(TwfDeferEty twfDeferEty) {

        TworkFormLogEty etyLog = new TworkFormLogEty();
        SysUser user = ShiroUtils.getSysUser();
        etyLog.setWfId(twfDeferEty.getWfId()); //[tWorkFormBase]PkId
        etyLog.setOpDeptNo(user.getDeptId().toString());
        etyLog.setOpDeptName(user.getDeptName());
        etyLog.setOpUserNo(user.getUserNo());
        etyLog.setOpUserName(user.getUserName());
        etyLog.setOpAction("审查延期申请");
        etyLog.setOpBtnId(-1);
        //设置延期申请的审核信息
        twfDeferEty.setCheckTime(DateUtils.getNowDate());
        twfDeferEty.setChkUserNo(user.getUserNo());
        twfDeferEty.setChkUserName(user.getUserName());
        String desc = "";
        if (twfDeferEty.getDeferState() != 22) {
            desc = "通过";
            TworkFormBaseEty tworkFormBaseEty = workFormBaseService.getById(twfDeferEty.getWfId());
            if (tworkFormBaseEty != null) {
                tworkFormBaseEty.setWfDefer(twfDeferEty.getDelayTo());
                workFormBaseService.updateById(tworkFormBaseEty);
            }
            TwfSendBackEty twfSendBackEty = wfSendBackService.getById(twfDeferEty.getSendId());
            if (twfSendBackEty != null) {
                twfSendBackEty.setBackLimit(twfDeferEty.getDelayTo());
                wfSendBackService.updateById(twfSendBackEty);
            }
        } else {
            desc = "不通过";
        }
        desc = "审核" + twfDeferEty.getAdeptName() + "延期申请（" + desc + "）";
        etyLog.setWsDesc(desc); //Show In website
        etyLog.setWsOpen(1); //website ShowFlag
        etyLog.setOpDesc(desc + "：" + twfDeferEty.getChkDesc());
        etyLog.setEffectFlag(0);
        iTworkFormLogService.save(etyLog);
        return toAjax(wfDeferService.updateById(twfDeferEty));
    }

    /**
     * 延期申请已审 查看页面
     *
     * @param pkId
     * @param map
     * @return
     */
    @GetMapping("/cmnFile/tDeferChkNView/{pkId}")
    public ModelAndView tDeferChkNView(@PathVariable("pkId") Integer pkId, ModelMap map) {
        SysUser user = ShiroUtils.getSysUser();
        TwfDeferEty twfDeferEty = wfDeferService.getById(pkId);
        twfDeferEty.setChkUserName(user.getUserName());
        twfDeferEty.setChkUserNo(user.getUserNo());
        TworkFormBaseEty tworkFormBaseEty = workFormBaseService.getById(twfDeferEty.getWfId());
        TwfSendBackEty twfSendBackEties = wfSendBackService.getById(twfDeferEty.getSendId());
        if (tworkFormBaseEty != null) {
            map.put("fmContent", tworkFormBaseEty.getFmContent());
            map.put("topic", tworkFormBaseEty.getWsTopic());
        }
        if (twfSendBackEties != null) {
            map.put("sendDesc", twfSendBackEties.getSendDesc());
        }
        map.put("twfDefer", twfDeferEty);
        map.put("delayRn", twfDeferEty.getDelayRn());
        map.put("adeptName", twfDeferEty.getAdeptName());
        map.put("auserName", twfDeferEty.getAuserName());
        map.put("limitOld", twfDeferEty.getLimitOld());
        return new ModelAndView(prefix + "/cmnFile/tDeferCheckView");
    }

    /**
     * 信件延期申请 未审 页面
     */
    @GetMapping("/cmnFile/mDeferChkNList")
    public ModelAndView mDeferChkNList() {
        return new ModelAndView(prefix + "/cmnFile/mDeferChkNList");
    }

    /**
     * 信件延期申请 未审 列表数据
     */
    @PostMapping("cmnFile/mDeferChkNListJson")
    public TableDataInfo mDeferChkNListJson(MwfDeferEty mwfDeferEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<MwfDeferEty> list;
        if (IS_GAOXIN()){
            list = mwfDeferService.mDeferChkNListJson_gxq(mwfDeferEty, user.getDeptId().toString());
        }else{
            list = mwfDeferService.mDeferChkNListJson(mwfDeferEty, user.getDeptId().toString());
        }
        return getDataTable(list);
    }

    /**
     * 信件延期申请 未审 处理页面
     *
     * @return
     */
    @GetMapping("cmnFile/mDeferCheck/{pkId}")
    public ModelAndView mDeferCheck(@PathVariable Integer pkId, ModelMap map) {
        SysUser user = ShiroUtils.getSysUser();
        MwfDeferEty mwfDeferEty = mwfDeferService.getById(pkId);
        mwfDeferEty.setChkUserName(user.getUserName());
        mwfDeferEty.setChkUserNo(user.getUserNo());
        MmailWorkForm mmailWorkForm = mmailWorkFormService.getById(mwfDeferEty.getWfId());
        MwfSendBack mwfSendBack = mwfSendBackService.getById(mwfDeferEty.getSendId());
        if (mmailWorkForm != null) {
            map.put("fmContent", mmailWorkForm.getFmContent());
            map.put("topic", mmailWorkForm.getFromTopic());
        }
        if (mwfSendBack != null) {
            map.put("sendDesc", mwfSendBack.getSendDesc());
            map.put("sortCode", mwfSendBack.getSortCode());
        }
        map.put("mwfDefer", mwfDeferEty);
        map.put("delayRn", mwfDeferEty.getDelayRn());
        map.put("adeptName", mwfDeferEty.getAdeptName());
        map.put("auserName", mwfDeferEty.getAuserName());
        map.put("limitOld", mwfDeferEty.getLimitOld());
        map.put("checkTime", new Date());
        return new ModelAndView(prefix + "/cmnFile/mDeferCheck");
    }

    /**
     * 信件延期处理 未审 审核数据
     */
    @PostMapping("cmnFile/mDeferCheckJson")
    @RequestBody
    public AjaxResult mDeferCheckJson(MwfDeferEty mwfDeferEty) {
        if (mwfDeferEty.getDeferState() != 22) {
            MwfSendBack mwfSendBack = mwfSendBackService.getById(mwfDeferEty.getSendId());
            if (mwfSendBack != null) {
                if (!mwfSendBack.getSortCode().equals("21")) {
                    UpdateWrapper<MmailWorkForm> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.set("wfDefer", mwfDeferEty.getDelayTo()).eq("PkId", mwfDeferEty.getWfId());
                    mmailWorkFormService.update(null, updateWrapper);
                }
                UpdateWrapper<MwfSendBack> updateWrapper = new UpdateWrapper<>();
                updateWrapper.set("BackLimit", mwfDeferEty.getDelayTo()).eq("PkId", mwfDeferEty.getSendId());
                mwfSendBackService.update(null, updateWrapper);
            }
        }
        boolean flag = mwfDeferService.updateById(mwfDeferEty);
        if (flag) {
            MMailWorkFormLog etyLog = new MMailWorkFormLog();
            SysUser user = ShiroUtils.getSysUser();
            etyLog.setWfId(mwfDeferEty.getWfId());
            etyLog.setOpDeptNo(user.getDeptId().toString());
            etyLog.setOpDeptName(user.getDeptName());
            etyLog.setOpUserNo(user.getUserNo());
            etyLog.setOpUserName(user.getUserName());

            etyLog.setOpAction("审查延期申请");
            etyLog.setOpBtnID(-1);
            etyLog.setLogTime(new Date());
            String desc = "";
            if (mwfDeferEty.getDeferState() != 22)
                desc = "通过";
            else {
                desc = "不通过";
            }
            desc = "审核" + mwfDeferEty.getAdeptName() + "延期申请（" + desc + "）";

            etyLog.setWsDesc(desc);
            etyLog.setWsOpen(1);
            etyLog.setOpDesc(desc + "：" + mwfDeferEty.getChkDesc());
            etyLog.setEffectFlag(0);
            mMailWorkFormLogService.save(etyLog);
        }
        return toAjax(flag);
    }

    /**
     * 信件延期申请 已审 页面
     */
    @GetMapping("/cmnFile/mDeferChkYList")
    public ModelAndView mDeferChkYList() {
        return new ModelAndView(prefix + "/cmnFile/mDeferChkYList");
    }

    /**
     * 信件延期申请 已审 列表数据
     */
    @PostMapping("/cmnFile/mDeferChkYListJson")
    public TableDataInfo mDeferChkYListJson(MwfDeferEty mwfDeferEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<MwfDeferEty> list = mwfDeferService.mDeferChkYListJson(mwfDeferEty, user.getDeptId().toString());
        return getDataTable(list);
    }

    /**
     * 信件待本部门回复 延期页面
     */
    @GetMapping("/cmnFile/mwfDeferList")
    public ModelAndView mwfDeferList(String sendId, String wfId, ModelMap map) {
        map.addAttribute("sendId", sendId);
        map.addAttribute("wfId", wfId);
        return new ModelAndView(prefix + "/cmnFile/mwfDeferList");
    }

    /**
     * 信件待本部门回复 延期数据
     */
    @PostMapping("cmnFile/mwfDeferListJson")
    public TableDataInfo mwfDeferListJson(MwfDeferEty mwfDeferEty, String sendId) {
        startPage();
        List<Map<Object, String>> list = mwfDeferService.mwfDeferListJson(mwfDeferEty, sendId);
        return getDataTable(list);
    }

    /**
     * 信件待本部门回复 延期新增页面
     */
    @GetMapping("/cmnFile/mwfDeferAdd")
    public ModelAndView mwfDeferAdd(Integer wfId, Integer sendId, ModelMap map) {
        MmailWorkForm mmailWorkForm = mmailWorkFormService.getById(wfId);
        MwfSendBack mwfSendBack = mwfSendBackService.getById(sendId);
        SysUser user = ShiroUtils.getSysUser();
        map.put("wfId", wfId);
        map.put("sendId", sendId);
        map.put("mmailWorkForm", mmailWorkForm);
        map.put("mwfSendBack", mwfSendBack);
        map.put("user", user);
        return new ModelAndView(prefix + "/cmnFile/mwfDeferAdd");
    }

    /**
     * 信件待本部门回复 延期新增数据
     *
     * @param mwfDeferEty
     * @return
     */
    @PostMapping("/cmnFile/mwfDeferAddJson")
    @RequestBody
    @RepeatSubmit
    public AjaxResult mwfDeferAddJson(MwfDeferEty mwfDeferEty) {
        boolean flag = mwfDeferService.save(mwfDeferEty);
        if (flag) {
            addDeferLog(getSysUser(),mwfDeferEty);
        }
        return toAjax(flag);
    }

    /**
     * 记录 信件新增延期申请 操作日志
     * @param user 当前用户
     * @param mwfDeferEty 延期实体信息
     */
    private void addDeferLog(SysUser user,MwfDeferEty mwfDeferEty) {
        MMailWorkFormLog etyLog = new MMailWorkFormLog();
        etyLog.setWfId(mwfDeferEty.getWfId());
        etyLog.setOpDeptNo(user.getDeptId().toString());
        etyLog.setOpDeptName(user.getDeptName());
        etyLog.setOpUserNo(user.getUserNo());
        etyLog.setOpUserName(user.getUserName());
        etyLog.setOpAction("提交延期申请");
        etyLog.setOpBtnID(-1);
        etyLog.setLogTime(new Date());
        String desc = "";
        desc = user.getDeptName() + "向（" + mwfDeferEty.getSdeptName() + "）提交延期申请";
        etyLog.setWsDesc(desc);
        etyLog.setWsOpen(1);
        etyLog.setOpDesc(desc);
        etyLog.setEffectFlag(0);
        mMailWorkFormLogService.save(etyLog);
    }

    /**
     * 信件待本部门回复 延期修改页面
     */
    @GetMapping("/cmnFile/mwfDeferEdit/{pkId}")
    public ModelAndView mwfDeferEdit(@PathVariable Integer pkId, ModelMap map) {
        MwfDeferEty mwfDeferEty = mwfDeferService.getById(pkId);
        map.put("mwfDefer", mwfDeferEty);
        MmailWorkForm mmailWorkForm = mmailWorkFormService.getById(mwfDeferEty.getWfId());
        map.put("mmailWorkForm", mmailWorkForm);
        MwfSendBack mwfSendBack = mwfSendBackService.getById(mwfDeferEty.getSendId());
        map.put("mwfSendBack", mwfSendBack);
        return new ModelAndView(prefix + "/cmnFile/mwfDeferEdit");
    }

    /**
     * 信件待本部门回复 延期修改数据
     *
     * @param mwfDeferEty
     * @return
     */
    @PostMapping("/cmnFile/mwfDeferEditJson")
    @RequestBody
    @RepeatSubmit
    public AjaxResult mwfDeferEditJson(MwfDeferEty mwfDeferEty) {
        return toAjax(mwfDeferService.updateById(mwfDeferEty));
    }

    /**
     * 信件待本部门回复 延期详情页面
     */
    @GetMapping("/cmnFile/deferDetail/{pkId}")
    public ModelAndView deferDetail(@PathVariable Integer pkId, ModelMap map) {
        MwfDeferEty mwfDeferEty = mwfDeferService.getById(pkId);
        map.put("mwfDefer", mwfDeferEty);
        MmailWorkForm mmailWorkForm = mmailWorkFormService.getById(mwfDeferEty.getWfId());
        map.put("mmailWorkForm", mmailWorkForm);
        MwfSendBack mwfSendBack = mwfSendBackService.getById(mwfDeferEty.getSendId());
        map.put("mwfSendBack", mwfSendBack);
        return new ModelAndView(prefix + "/cmnFile/deferDetail");
    }

    /**
     * 延期申请
     * 根据id 进行多个删除和单个删除
     *
     * @return
     */
    @PostMapping("/cmnFile/removeMwfDeferJson")
    @RequestBody
    public AjaxResult removeMwfDeferJson(String... ids) {
        return toAjax(mwfDeferService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 电话批量申请延期页面
     * @param ids 派单ids
     */
    @GetMapping("/cmnFile/tDeferBatchAdd")
    public ModelAndView tDeferBatchAdd(String ids,ModelMap map){
        map.put("ids",ids);
        return new ModelAndView(prefix + "/cmnFile/tDeferBatchAdd");
    }


    /**
     * 电话批量新增延期申请
     * @param ids 需要新增延期的派单ids
     * @param days 延期天数
     * @return 影响结果
     */
    @PostMapping("/cmnFile/tDeferBatchAddJson")
    @RequestBody
    public AjaxResult tDeferBatchAddJson(@RequestParam("ids")String ids,@RequestParam("deferDays")Integer days,@RequestBody TwfDeferEty twfDeferEty) throws ParseException {
        //保存运行结果
        boolean flag = false;
        //派单id数组
        String[] idList = ids.split(",");
        for (String id : idList) {
            //构建延期申请详情信息
            TwfSendBackEty twfSendBackEty = wfSendBackService.getById(Integer.parseInt(id));
            TworkFormBaseEty tworkFormBaseEty = workFormBaseService.getById(twfSendBackEty.getWfId());
            twfDeferEty.setSendId(Integer.parseInt(id));
            twfDeferEty.setWfId(twfSendBackEty.getWfId());
            twfDeferEty.setSuserNo(twfSendBackEty.getSuserNo());
            twfDeferEty.setSuserName(twfSendBackEty.getSuserName());
            twfDeferEty.setFlagInt(0);
            twfDeferEty.setFlagString("");
            twfDeferEty.setSpareInt(0);
            twfDeferEty.setSpareString("");
            twfDeferEty.setWorkFormNo(tworkFormBaseEty.getWorkFormNo());
            twfDeferEty.setSdeptId(twfSendBackEty.getSdeptId());
            twfDeferEty.setSdeptName(twfSendBackEty.getSdeptName());
            twfDeferEty.setAdeptNo(twfSendBackEty.getSbdeptId());
            twfDeferEty.setAdeptName(twfSendBackEty.getSbdeptName());
            twfDeferEty.setAuserNo(getSysUser().getUserNo());
            twfDeferEty.setAuserName(getSysUser().getUserName());
            twfDeferEty.setLimitOld(twfSendBackEty.getBackLimit());
            //设置延期天数
            String deferDate = commonService.nextXWorkdayGet(days,DateUtils.parseDateToStr("yyyy-MM-dd",twfSendBackEty.getBackLimit()));
            twfDeferEty.setDelayTo(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(deferDate));
            //新增延期申请
            flag = wfDeferService.save(twfDeferEty);
            //记录 新增延期 操作日志
            addDeferLog(getSysUser(),twfDeferEty);
        }
        return toAjax(flag);
    }

    /**
     * 记录 电话新增延期申请 操作日志
     * @param user 当前用户
     * @param twfDeferEty 延期实体信息
     */
    private void addDeferLog(SysUser user,TwfDeferEty twfDeferEty) {
        TworkFormLogEty etyLog = new TworkFormLogEty();
        //[tWorkFormBase]PkId
        etyLog.setWfId(twfDeferEty.getWfId());
        etyLog.setOpDeptNo(user.getDept().getNoUnit());
        etyLog.setOpDeptName(user.getDeptName());
        etyLog.setOpUserNo(user.getUserNo());
        etyLog.setOpUserName(user.getUserName());
        etyLog.setOpAction("提交延期申请");
        etyLog.setOpBtnId(-1);
        TwfSendBackEty twfSendBackEty = wfSendBackService.getById(twfDeferEty.getSendId());
        String desc = "向（" + twfSendBackEty.getSdeptName() + "）提交延期申请";
        etyLog.setWsDesc(desc); //Show In website
        etyLog.setWsOpen(0); //website ShowFlag//2014-3-27 1->0
        etyLog.setOpDesc(desc);
        etyLog.setEffectFlag(0);
        iTworkFormLogService.save(etyLog);
    }

    /**
     * 信件批量申请延期页面
     * @param ids 派单ids
     */
    @GetMapping("/cmnFile/mDeferBatchAdd")
    public ModelAndView mDeferBatchAdd(String ids,ModelMap map){
        map.put("ids",ids);
        return new ModelAndView(prefix + "/cmnFile/mDeferBatchAdd");
    }

    /**
     * 信件批量新增延期申请
     * @param ids 需要新增延期的派单ids
     * @param days 延期天数
     * @return 影响结果
     */
    @PostMapping("/cmnFile/mDeferBatchAddJson")
    @RequestBody
    public AjaxResult mDeferBatchAddJson(@RequestParam("ids")String ids,@RequestParam("deferDays")Integer days,@RequestBody MwfDeferEty mwfDeferEty) throws ParseException {
        //保存运行结果
        boolean flag = false;
        //派单id数组
        String[] idList = ids.split(",");
        for (String id : idList) {
            //构建延期申请详情信息
            MwfSendBack mwfSendBackEty = mwfSendBackService.getById(Integer.parseInt(id));
            MmailWorkForm mworkFormBaseEty = mmailWorkFormService.getById(mwfSendBackEty.getWfId());
            mwfDeferEty.setSendId(Integer.parseInt(id));
            mwfDeferEty.setWfId(mwfSendBackEty.getWfId());
            mwfDeferEty.setSuserNo(mwfSendBackEty.getSuserNo());
            mwfDeferEty.setSuserName(mwfSendBackEty.getSuserName());
            mwfDeferEty.setFlagInt(0);
            mwfDeferEty.setFlagString("");
            mwfDeferEty.setSpareInt(0);
            mwfDeferEty.setSpareString("");
            mwfDeferEty.setWorkFormNo(mworkFormBaseEty.getWorkFormNo());
            mwfDeferEty.setSdeptId(mwfSendBackEty.getSdeptId());
            mwfDeferEty.setSdeptName(mwfSendBackEty.getSdeptName());
            mwfDeferEty.setAdeptNo(mwfSendBackEty.getSbdeptId());
            mwfDeferEty.setAdeptName(mwfSendBackEty.getSbdeptName());
            mwfDeferEty.setAuserNo(getSysUser().getUserNo());
            mwfDeferEty.setAuserName(getSysUser().getUserName());
            mwfDeferEty.setLimitOld(mwfSendBackEty.getBackLimit());
            //设置延期天数
            String deferDate = commonService.nextXWorkdayGet(days,DateUtils.parseDateToStr("yyyy-MM-dd",mwfSendBackEty.getBackLimit()));
            mwfDeferEty.setDelayTo(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(deferDate));
            //新增延期申请
            flag = mwfDeferService.save(mwfDeferEty);
            //记录 新增延期 操作日志
            addDeferLog(getSysUser(),mwfDeferEty);
        }
        return toAjax(flag);
    }


    /**
     * 电话批量审核延期页面
     * @param ids 延期记录ids
     */
    @GetMapping("/cmnFile/tDeferBatchCheck")
    public ModelAndView tDeferBatchCheck(String ids,ModelMap map){
        map.put("ids",ids);
        return new ModelAndView(prefix + "/cmnFile/tDeferBatchCheck");
    }


    /**
     * 电话批量审核延期申请
     * @param ids 需要审核延期的延期ids
     * @param twfDeferEty 保存审核信息
     * @return 影响结果
     */
    @PostMapping("/cmnFile/tDeferBatchCheckJson")
    @RequestBody
    public AjaxResult tDeferBatchCheckJson(@RequestParam("ids")String ids,@RequestBody TwfDeferEty twfDeferEty) throws ParseException {
        //保存运行结果
        AjaxResult res = AjaxResult.warn("操作失败");
        //派单id数组
        String[] idList = ids.split(",");
        for (String id : idList) {
            //设置审核后延期实体信息
            TwfDeferEty deferEty = wfDeferService.getById(Integer.parseInt(id));
            deferEty.setChkDesc(twfDeferEty.getChkDesc());
            deferEty.setChkRemark(twfDeferEty.getChkRemark());
            deferEty.setDeferState(twfDeferEty.getDeferState());
            //调用审核延期申请方法
            res = tDeferCheckJson(deferEty);
        }
        return res;
    }

    /**
     * 信件批量审核延期页面
     * @param ids 延期记录ids
     */
    @GetMapping("/cmnFile/mDeferBatchCheck")
    public ModelAndView mDeferBatchCheck(String ids,ModelMap map){
        map.put("ids",ids);
        return new ModelAndView(prefix + "/cmnFile/mDeferBatchCheck");
    }

    /**
     * 电话批量审核延期申请
     * @param ids 需要审核延期的延期ids
     * @param mwfDeferEty 保存审核信息
     * @return 影响结果
     */
    @PostMapping("/cmnFile/mDeferBatchCheckJson")
    @RequestBody
    public AjaxResult mDeferBatchCheckJson(@RequestParam("ids")String ids,@RequestBody MwfDeferEty mwfDeferEty) throws ParseException {
        //保存运行结果
        AjaxResult res = AjaxResult.warn("操作失败");
        //派单id数组
        String[] idList = ids.split(",");
        for (String id : idList) {
            //设置审核后延期实体信息
            MwfDeferEty deferEty = mwfDeferService.getById(Integer.parseInt(id));
            deferEty.setChkDesc(mwfDeferEty.getChkDesc());
            deferEty.setChkRemark(mwfDeferEty.getChkRemark());
            deferEty.setDeferState(mwfDeferEty.getDeferState());
            deferEty.setCheckTime(DateUtils.getNowDate());
            deferEty.setChkUserNo(getSysUser().getUserNo());
            deferEty.setChkUserName(getSysUser().getUserName());
            //调用审核延期申请方法
            res = mDeferCheckJson(deferEty);
        }
        return res;
    }
}
